package com.sp.scmdb.common.action;

import java.lang.reflect.ParameterizedType;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;

/**
 * 
 * @author lzj
 */
@SuppressWarnings("unchecked")
public class BaseAction<T extends Object> extends ActionSupport implements ModelDriven<T>, Preparable, SessionAware,
		ServletRequestAware, ServletResponseAware {
	private static final long serialVersionUID = 1L;

	protected HttpServletRequest request;

	protected HttpServletResponse response;

	protected Map session;

	protected String pageName;

	protected T entity;

	public String getPageName() {
		return pageName;
	}

	public void setPageName(String pageName) {
		this.pageName = pageName;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public T getEntity() {
		return entity;
	}

	public void prepare() throws Exception {
		this.entity = getEntityClass();
	}

	public void setEntity(T entity) {
		this.entity = entity;
	}

	public T getModel() {
		return entity;
	}

	public T getEntityClass() {
		T t = null;
		try {
			t = ((Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0])
					.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return t;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;

	}

	public void messageSession(String CHECK_ERRORFLAG, String TYPE) {
		session.put("TITLE", "操作信息");
		session.put("URL", "");

		if (CHECK_ERRORFLAG.equals(CommonParamter.CHECK_FLG)) {
			session.put("MSG", TYPE);
			session.put("CHECK_ERRORFLAG", CommonParamter.CHECK_FAIL);
		} else if (CHECK_ERRORFLAG.equals(CommonParamter.CHECK_SUCCESS)) {
			session.put("MSG", CommonParamter.OP_SUCCESS);
			session.put("CHECK_ERRORFLAG", CommonParamter.CHECK_SUCCESS);
		} else if (CHECK_ERRORFLAG.equals(CommonParamter.CHECK_FAIL)) {
			session.put("MSG", CommonParamter.OP_FAIL);
			session.put("CHECK_ERRORFLAG", CommonParamter.CHECK_FAIL);
		} else {
			session.put("MSG", TYPE + CommonParamter.OP_REPEAT);
			session.put("CHECK_ERRORFLAG", CommonParamter.CHECK_FAIL);
		}
	}

	public void button(String urlAdd, String urlEdit, String urlDel) {
		UserBean user = (UserBean) session.get("user");
		StringBuffer button = new StringBuffer();
		if (StringUtils.isNotEmpty(urlAdd)) {
			if (user.getRole().getOperation().startsWith("添加") || user.getRole().getOperation().startsWith("全部")) {
				button.append("<input name=\"添加\" type=\"button\" class=\"bAdd\" value=\"添加\" onclick=\"" + urlAdd
						+ "\" />&nbsp;");
			}
		}
		if (StringUtils.isNotEmpty(urlEdit)) {
			if (user.getRole().getOperation().startsWith("修改") || user.getRole().getOperation().startsWith("全部")) {
				button.append("<input name=\"修改\" type=\"button\" class=\"bEdit\" value=\"修改\" onclick=\"" + urlEdit
						+ "\" />&nbsp;");
			}
		}
		if (StringUtils.isNotEmpty(urlDel)) {
			if (user.getRole().getOperation().startsWith("删除") || user.getRole().getOperation().startsWith("全部")) {
				button.append("<input name=\"删除\" type=\"button\" class=\"bDel\" value=\"删除\" onclick=\"" + urlDel
						+ "\" />");
			}
		}
		request.setAttribute("button", button.toString());
	}
}
